﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using S.EarTag.API.Mediator.MediatorCommand;
using S.EarTag.API.Mediator.MediatorCommand.EarTagCommand;
using S.EarTag.Domain.Model;
using Serilog;
using Authorization.Center.Controllers;
using Authorization.Center.SqlServer;
using S.EarTag.API.Application.MediatorCommand.MarkCommand;

namespace S.EarTag.API.Controllers
{
    /// <summary>
    /// 耳标管理——归光阳
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class EarTagController : BaseController
    {
        private readonly IMediator _mediator;
        public EarTagController(IMediator mediator,MySqlDbContext _db):base(_db)
        {
            _mediator= mediator;
        }
        [HttpPost]
        public IActionResult cs()
        {
            return Ok(Convert.ToInt32("123asa"));
        }
        /// <summary>
        /// 添加耳标
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public async Task<IActionResult> AddEarTag(EarTagCreateCommand command)
        {
            command.CreateBy = UserStrategyDto.UserTrueName;
            command.CreateDate = DateTime.Now;
            command.UpdateBy = "";
            command.UpdateDate = "";
            command.EarTagType = "可视耳号";
            var result=await _mediator.Send(command);
            return Ok(result);
        }
        /// <summary>
        /// 修改耳标
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public async Task<IActionResult> UpdEarTag(EarTagUpdateCommand command)
        {
            command.UpdateBy = UserStrategyDto.UserTrueName;
            command.UpdateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            var result=await _mediator.Send(command);
            return Ok(result);
        }
        /// <summary>
        /// 根据Id删除耳标
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public async Task<IActionResult> DeleteEarTag(int id)
        {
            EarTagDeleteCommand command=new EarTagDeleteCommand();
            command.EarTagId=id;
            var result=await _mediator.Send(command);
            return Ok(result);
        }
        /// <summary>
        /// 批量添加耳标
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public async Task<IActionResult> BathAddEarTag(List<EarTagModel> model)
        {
            EarTagBatchAddCommand command=new EarTagBatchAddCommand();
            var result = await _mediator.Send(new EarTagBatchAddCommand(){ model= model});
            return Ok(result);
        }
        /// <summary>
        /// 批量删除耳标
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public async Task<IActionResult> BathRemoveEarTag(EarTagBatchRemoveCommand command)
        {
            var result=await _mediator.Send(command);
            return Ok(result);
        }
        /// <summary>
        /// 根据耳标Id修改状态(用来绑定对应电子耳号)
        /// </summary>
        /// <param name="EarTagId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UpdDateEatTag(int EarTagId)
        {
            Log.Debug("方法开始");
            EarTagUpdDateCommand command =new EarTagUpdDateCommand();
            command.EarTagId=EarTagId;
            var result=await _mediator.Send(command);
            return Ok(result);
        }
        /// <summary>
        /// 修改绑定后可视耳号为已使用（禁用）
        /// </summary>
        /// <param name="EarTag"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> GetEraTagState(string EarTag)
        {
            EarTagUpdDateStateCommand command=new EarTagUpdDateStateCommand();
            command.EarTag = EarTag;
            var list=await _mediator.Send(command);
            return Ok(list);
        }
        /// <summary>
        /// 根据可视耳号修改状态为未使用
        /// </summary>
        /// <param name="EarTag"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UpdMarkStateTwo(string EarTag)
        {
            MarkStateEarTagTwoCommand command =new MarkStateEarTagTwoCommand();
            command.EarTag = EarTag;
            var result=await _mediator.Send(command);
            return Ok(result);
        }
    }
}
